<?php
/**
 Template Name:data_init
 * @package WordPress
 * @subpackage Default_Theme
 * @author: shiwei
 */

	$keyword = urldecode($_REQUEST['keyword']);
	if($keyword==null || ""==$keyword){
		$keyword = $_GET['keyword'];
		if($keyword==null || ""==$keyword){
			$keyword = $_POST['keyword'];
		}
	}
	
	$cat = urldecode($_REQUEST['cat']); 
	if($cat==null || ""==$cat){
		$cat = $_GET['cat'];
		if($cat==null || ""==$cat){
			$cat = $_POST['cat'];
		}
	}
	
	$callback = $_GET['callback'];
	$postQue = "" ;
	if($cat != null && "" != $cat){
		$postQue = "select post.post_title post_title ".
			" from  wp_term_taxonomy tax ".
			" left join wp_term_relationships ship ".
			" on ship.term_taxonomy_id = tax.term_taxonomy_id ".
			" left join wp_posts post ".
			" on post.ID = ship.object_id ".
			" where tax.taxonomy = 'category' ".
			" and  post.post_status = 'publish' and post.post_type='post' ".
			" and tax.parent='".$cat."' ";
	}else{
		$postQue = "select post.post_title post_title from wp_posts post ".
		" where post.post_status='publish' and post.post_type='post'  " ;
	}
	
	if($keyword != null && "" != $keyword){
		$postQue = $postQue." and post.post_title like  '%".$keyword."%' " ;
	}
	$postQue = $postQue."  limit 0 ,10" ;
	
	$posts = $wpdb->get_results($postQue);
	
	$ccc = "[ " ;
	foreach($posts as $post){
		$ccc.="{name:'".$post->post_title."'}," ;
	}
	//$ccc.="{name:'".$keyword."'}," ;
	$ccc = substr($ccc, 0 , $ccc.length -1) ;
	$ccc.="] " ;
	echo $callback.'('.$ccc.')' ;
?>